package com.micro.school.h5.controller;

import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.h5.domin.StudentH5Login;
import com.micro.school.h5.service.StudentH5LoginService;
import com.micro.school.h5.service.WeChatService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.vo.*;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studentH5")
public class StudentLoginController {

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private StudentH5LoginService studentH5LoginService;



    @Autowired
    private WeChatService weChatService;



    /**
     * 通过微信code获取openId
     *
     * @param code 微信登录凭证code
     * @return openId
     */
    @GetMapping("/getOpenId")
    public AjaxResult getOpenId(@RequestParam("code") String code) {
        try {
            String openId = weChatService.getOpenId(code);
            if (openId != null) {
                return AjaxResult.success(openId);
            } else {
                return AjaxResult.error("获取openId失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("获取openId异常: " + e.getMessage());
        }
    }

    /**
     * H5登录
     * @param studentInfoVos
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody StudentInfoVos studentInfoVos) {
        if (studentInfoVos.getOpenId() == null){
            return AjaxResult.error("登录失败！请先通过微信认证");
        }
        R<StudentInfos> studentInfosR = remoteUserService.queryStudent(studentInfoVos);
        if (studentInfosR.getData() != null ){
            //登录成功后需要绑定登录内容信息
            studentH5LoginService.loginStudentLogin(studentInfosR.getData(), studentInfoVos);
            return AjaxResult.success(studentInfosR.getData());
        }else{
            return AjaxResult.error("登录失败！请核验学生信息");
        }
    }

    /**
     * 展示学生信息
     * @param studentInfoVos
     * @return
     */
    @PostMapping("/showStudent")
    public AjaxResult showStudent(@RequestBody StudentInfoVos studentInfoVos) {
        R<StudentInfos> studentInfosR = remoteUserService.queryStudent(studentInfoVos);
        StudentInfos data = studentInfosR.getData();
        return AjaxResult.success(data);
    }


    /**
     * 展示微信openId对应的学生信息
     * @param openId
     * @return
     */
    @GetMapping("/showStudentList")
    public AjaxResult showStudent(String openId) {
        List<StudentH5Login> studentH5LoginList = studentH5LoginService.queryLoginList(openId);
        return AjaxResult.success(studentH5LoginList);
    }


    /**
     * 删除微信openId对应的某个学生信息
     * @param id
     * @return
     */
    @GetMapping("/deleteLoginStudent")
    public AjaxResult deleteLoginStudent(Integer id) {
        studentH5LoginService.deleteLoginStudent(id);
        return AjaxResult.success("删除成功");
    }

}
